home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Gold Collection / Software Vault - The Gold Collection (American Databankers) (1993).ISO / cdr52 / ntx2.zip / NTXREC.C < prev    next >
Text File  |  1993-04-13  |  5KB  |  233 lines

  1. //.............................................................................
  2. //
  3. //   Program Name: NTXREC.C          Copyright: RCM Software Pty. Ltd.                                   
  4. //   Date Created: 03/05/91           Language: Microsoft C 5.1                                         
  5. //   Time Created: 13:21:00             Author: Graham D. McKechnie                       
  6. // 
  7. //   Modified 9/25/92 7:00 pm
  8. //   by Brian Marasca
  9. //   Use index handle rather than file name.
  10. //   IndexOrd() is now passed as parameter 1.
  11. // 
  12. //   Returns dbf record number from index position
  13. //   eg.
  14. //   nNtxPos   := 677
  15. //   nDbfRecNo := NtxRec( IndexOrd(), nNtxPos )
  16. //.............................................................................
  17.  
  18. #include "stdio.h"
  19. #include "extend.h"
  20.  
  21. #define open  _topen                // Use Clipper's internals
  22. #define close _tclose               // Don't have to link LLIBCA
  23. #define lseek _tlseek
  24. #define read  _tread
  25.  
  26. extern int  _topen( char*, int);
  27. extern int  _tclose( int );
  28. extern long _tlseek( int, long, int);
  29. extern int  _tread( int, char*, int);
  30.  
  31. #define BUFF_SIZE   1024
  32. #define ERROR       -1
  33. #define MAX_KEY     256
  34. #define O_RDONLY    0x0000  // open for reading only 
  35. #define O_BINARY    0x8000  // file mode is binary (untranslated) 
  36.  
  37. // Index structures 
  38. typedef struct
  39. {
  40.    unsigned  uSign;
  41.    unsigned  uVersion;
  42.    long      lRoot;
  43.    long      lNextPage;
  44.    unsigned  uItemSize;
  45.    unsigned  uKeySize;
  46.    unsigned  uKeyDec;
  47.    unsigned  uMaxItem;
  48.    unsigned  uHalfPage;
  49.    char      cKeyExpr[MAX_KEY];
  50.    Boolean   bUnique;
  51. } NTXHEADER;
  52.  
  53.  
  54. typedef struct
  55. {
  56.    long lPage;
  57.    long lRecNo;
  58.    char cKey;
  59. } ITEM;
  60.  
  61.  
  62. typedef struct
  63. {
  64.    unsigned uCount;
  65.    unsigned uRef;
  66. } BUFFER;
  67.  
  68.  
  69.  
  70.  
  71.  
  72. static Boolean bError = FALSE;
  73. static Boolean bFound = FALSE;
  74.  
  75. static long lRetVal;            // Return value
  76. static long lNtxPos;            // Index position
  77. static int  nNtxHandle;         // Index file handle
  78.  
  79.  
  80.  
  81. CLIPPER ntxrec()
  82.  
  83. {
  84.  
  85.    long lNtxRecNo;
  86.    int nNtxOrd;
  87.    long nNtxPtr;
  88.  
  89.    NTXHEADER NtxHeader;
  90.  
  91.  
  92.    // Check the parameters passed from Clipper
  93.    if ( PCOUNT != 2 )
  94.    {
  95.       _retni(-1);
  96.       return;
  97.    }
  98.  
  99.    if ( ! (ISNUM(1) && ISNUM(2) ) )
  100.    {
  101.       _retni(-1);
  102.       return;
  103.    }
  104.  
  105.  
  106.    nNtxOrd   = _parni(1);    // IndexOrd() to search
  107.    lNtxRecNo = _parnl(2);    // RECNO() we are looking for
  108.  
  109.    lRetVal   = 0L;           // Set these each time
  110.    lNtxPos   = 0L;
  111.    bFound    = FALSE;
  112.  
  113.    if ( ( nNtxHandle = getNtxHandle ( nNtxOrd ) ) != -1 )
  114.    {
  115.         /* save the ntx file position and reset it to the top */
  116.         nNtxPtr = _tlseek( nNtxHandle, 0L, SEEK_CUR );
  117.         _tlseek( nNtxHandle, 0L, SEEK_SET );
  118.  
  119.         // read the header 
  120.         if ( read ( nNtxHandle, (char *) &NtxHeader, sizeof(NtxHeader) )
  121.                        != sizeof(NtxHeader) )
  122.            goto ERROR_EXIT; 
  123.  
  124.         // start the traversal from root 
  125.         DumpPage1( NtxHeader.lRoot, lNtxRecNo );
  126.  
  127.         if (bError)
  128.         {
  129.            ERROR_EXIT:
  130.            bError = TRUE;
  131.         }
  132.  
  133.         /* restore the ntx file position */
  134.         _tlseek( nNtxHandle, nNtxPtr, SEEK_SET );
  135.    }
  136.  
  137.    _retnl( lRetVal );
  138.  
  139. }
  140.  
  141.  
  142.  
  143. DumpPage1(long lPageOffSet, long lNtxRecNo)
  144.  
  145. {
  146.    char     *cPage;
  147.    ITEM     *item;
  148.    BUFFER   *buffer;
  149.    unsigned i;
  150.    unsigned *uItemRef;
  151.  
  152.    /* allocate page */
  153.    cPage = _xalloc(BUFF_SIZE);
  154.  
  155.    /* move to this position in the file */
  156.    if( lseek( nNtxHandle, lPageOffSet, 0 ) != (long)lPageOffSet )
  157.       goto DUMP_EXIT;
  158.  
  159.    /* read the page */
  160.    if( read( nNtxHandle, cPage, BUFF_SIZE) != BUFF_SIZE )
  161.       goto DUMP_EXIT;
  162.  
  163.  
  164.    buffer   = (BUFFER *) cPage;
  165.    uItemRef = &buffer -> uRef;
  166.  
  167.  
  168.    for (i = 0; i < buffer -> uCount; i++)
  169.    {
  170.       item = (ITEM *) &cPage[ uItemRef[ i ] ];
  171.  
  172.       if ( ! bFound )
  173.       {
  174.          if ( item -> lPage )
  175.             DumpPage1( item -> lPage, lNtxRecNo );
  176.  
  177.          if (bFound)
  178.          {
  179.             _xfree( cPage );
  180.             return;
  181.          }  
  182.          lNtxPos++;
  183.       }
  184.  
  185.       if ( lNtxPos == lNtxRecNo )
  186.       {
  187.  
  188.          lRetVal = item->lRecNo;
  189.          bFound = TRUE;
  190.          break;
  191.       }
  192.    }
  193.  
  194.    if ( ! bFound )    // Wasn't finding Williams properly
  195.    {
  196.       /* handle extra right pointer */
  197.       item = (ITEM *) &cPage[ uItemRef[ buffer -> uCount ] ];    
  198.       if ( item -> lPage )
  199.          DumpPage1(item -> lPage, lNtxRecNo );
  200.    }
  201.  
  202.    _xfree( cPage );
  203.  
  204.    if ( bError )
  205.    {
  206.       DUMP_EXIT:
  207.       _xfree( cPage );
  208.       bError = TRUE;
  209.    }
  210. }
  211.  
  212.  
  213. /**************************************************************************
  214.    FUNCTION: getNtxHandle ( n )
  215.    Returns the index handle for the current workarea, or -1 if error.
  216.    Brian Marasca 9/25/92
  217. **************************************************************************/
  218.  
  219. static int getNtxHandle ( n )
  220. int n ;
  221. {
  222.      extern char **_workareas ;
  223.      int **hp, han = -1 ;
  224.  
  225.      if(n > 0 && n <= 15)
  226.      {
  227.           hp = (int **) ((*_workareas) + 0x98 + ((n-1) * sizeof (char *))) ;
  228.           han = *hp ? **hp : -1 ;
  229.      }
  230.  
  231.      return han ;
  232. }
  233.